

// 到处删除
exports.deleteMember = async function(req,res,next) {
  const { id } = req.body;
  const connection = req.connection;
  const sql =  deleteSql(id)
  if(!sql) {
    return res.resend.sendJson(req, res, {
      status: 500,
      code: 'FAIL',
      msg: '删除失败, sql语句错误',
      data: {
        token: req.newToken
      }
    })
  }
  // 执行删除
  try {
    const [ result ] = await connection.query(sql, []);
    
    if(result.affectedRows > 0) {
      return res.resend.sendJson(req, res, {
        status: 200,
        code: 'SUCCESS',
        msg: '删除成功',
        data: {
          token: req.newToken
        }
      })
    } 
    return res.resend.sendJson(req, res, {
      status: 400,
        code: 'FAIL',
        msg: '没有找到对应的数据',
        data: {
          token: req.newToken
        }
    })
  }
  catch(err) {
  
    res.resend.sendJson(req, res, {
      status: 500,
      code: 'FAIL',
      msg: '删除失败',
      logMsg: err
    })
  }
}


// 依据id返回删除sql语句
function deleteSql(id) {
  if( Array.isArray(id) && id.length > 0) {
    return `DELETE FROM members WHERE id in (${id.join(',')})`
  }
  if( typeof id === 'number' || typeof id === 'string') {
    return `DELETE FROM members WHERE id = ${id}`
  }
  return ``
}